<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Site/classes/PhpFox_ComponentStyle.class.php, $Author:  $ */



/** Component for building site style
 * @version $Id: PhpFox_ComponentStyle.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 * @package module.site
 *
 */
class PhpFox_Mod_Site_ComponentStyle extends BaseComponent
{
    function process()
    {
        $oCssItem = &$this->_oModule->getItem('Css');
        /* @var $oCssItem PhpFox_Mod_Site_ItemCss */
        $oCache = &App::newObject("Cache");
        $oReq = &App::getRequest();
        $oTpl = &$this->_oModule->getTemplate();
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        if (!$oSecurityService->hasStaffPerm('MENU_css_default'))
        {
            App::gotoUrl('admin.main');
        }

        if ($iStyleId = $oReq->getInt('setdefault'))
        {
            $oCssItem->oDb->query('UPDATE '.App::getT('css').' '.
                                 ' SET `default` = "" '.
                                 ' WHERE `default` <> ""');
            $oCssItem->load($iStyleId);
            $oCssItem->aData['default'] = 1;
            $oCssItem->update(array('default'));

			setcookie("cssName","","-1","/");
			setcookie("cssFile","","-1","/");
			setcookie("cssLayout","","-1","/");

			$this->oDatabase->update(App::getT('sys_sett'), array('val' => '1'), "`code` = 'check_css_change'");
			App::deleteSetting();

            App::gotoUrl('admin.config.style');
        }
        if ($iStyleId = $oReq->getInt('delete'))
        {
            $oCssItem->load($iStyleId);
            if ( !$oCssItem->aData['default'] )
            {
                $oCssItem->delete($iStyleId);
            }
            $oCache->rmFile("css.php");

			setcookie("cssName","","-1","/");
			setcookie("cssFile","","-1","/");
			setcookie("cssLayout","","-1","/");

            App::gotoUrl('admin.config.style');
        }
        if ($oReq->getAction() == 'update')
        {
            if (($sName = $oReq->get('name')) && ($iStyleId = $oReq->getInt('id')))
            {
                $oCssItem->load($iStyleId);
                $oCssItem->aData['name'] = $sName;
                $oCssItem->update(array('name'));
            }
            $oCache->rmFile("css.php");
            App::gotoUrl('admin.config.style');
        }
        $sSort = '`default` DESC';

        $aItems = $this->oDatabase->getRows("
        	SELECT c.id, c.name, c.file, c.default, h.name AS layout FROM
        		". App::getT('css') ." AS c
        		LEFT JOIN ". App::getT('html_layout') ." AS h ON(h.id = c.html)
        	ORDER BY c.default DESC
        	");
		
		App::loadClass('file/File');
		$oFile = new CFile;
		foreach( $aItems as $iKey => $aValue )
		{
			$aItems[$iKey]['full_location'] = App::getParam('style.path') . $aItems[$iKey]['file'] . '/style.css';
			$aItems[$iKey]['can_edit'] = ($oFile->isWritable($aItems[$iKey]['full_location']) ? true : false);
		}

        $oTpl->assign(array(
            'aItems' => $aItems,
        ));
        return $oTpl->fetch('Style.html');
    }
}
?>